#include <windows.h>

BOOL WINAPI CreateInjectedProcessA(
  LPCTSTR lpApplicationName,
  LPTSTR lpCommandLine,
  LPSECURITY_ATTRIBUTES lpProcessAttributes,
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  BOOL bInheritHandles,
  DWORD dwCreationFlags,
  LPVOID lpEnvironment,
  LPCTSTR lpCurrentDirectory,
  LPSTARTUPINFO lpStartupInfo,
  LPPROCESS_INFORMATION lpProcessInformation,
  LPCTSTR lpDllPath
)
{
    DWORD dwWritten;
    LPVOID lpStringLoc;
    HANDLE hThread;

    if(CreateProcessA(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags|CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation))
    {
        if(lpDllPath)
        {
            lpStringLoc = VirtualAllocEx(lpProcessInformation->hProcess, 0, strlen(lpDllPath)+1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
            WriteProcessMemory(lpProcessInformation->hProcess, lpStringLoc, lpDllPath, strlen(lpDllPath)+1, &dwWritten);

            hThread = CreateRemoteThread(lpProcessInformation->hProcess, 0, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA"), lpStringLoc, 0, 0);
            WaitForSingleObject(hThread, INFINITE);
        }

        ResumeThread(lpProcessInformation->hThread);

        return TRUE;
    }

    return FALSE;
}
